//
//  ImageeCell.m
//  UI集合视图图片实现
//
//  Created by rimi on 16/8/17.
//  Copyright © 2016年 Miko. All rights reserved.
//

#import "ImageeCell.h"

@interface ImageeCell ()
@property(nonatomic,strong)UIButton* imageBtn;

@property(nonatomic,strong)UIButton* deleteImageBtn;

@end

@implementation ImageeCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //初始化内部视图
        _imageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _imageBtn.frame = self.bounds;
        [_imageBtn setBackgroundImage:[UIImage imageNamed:@"compose_pic_add"] forState:UIControlStateNormal];
        [_imageBtn setBackgroundImage:[UIImage imageNamed:@"compose_pic_add_highlighted"] forState:UIControlStateHighlighted];
        
        //监听
        [_imageBtn addTarget:self action:@selector(addImageBtnClicked) forControlEvents:UIControlEventTouchUpInside];
        
        //添加
        [self.contentView addSubview:_imageBtn];
        
        
        _deleteImageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _deleteImageBtn.frame = CGRectMake(CGRectGetMaxX(_imageBtn.frame) - 24, 0, 24, 24);
        [_deleteImageBtn setBackgroundImage:[UIImage imageNamed:@"compose_photo_close"] forState:UIControlStateNormal];
        [_deleteImageBtn addTarget:self action:@selector(deleteImageBtnClicked) forControlEvents:UIControlEventTouchUpInside];
        //设置默认隐藏
        _deleteImageBtn.hidden = YES;
        [self.contentView addSubview:_deleteImageBtn];
    }
    return self;
}

//添加图片按钮的回调方法
- (void)addImageBtnClicked{
    //3、
//    判断代理方是否实现了代理方法
    if ([self.delegate respondsToSelector:@selector(ImageeCellAddImageBtnClickedWithCell:)]) {
        //给代理发送消息
        [self.delegate ImageeCellAddImageBtnClickedWithCell:self];
    }
}

//删除按钮的回调方法
- (void)deleteImageBtnClicked{
    //判断代理方是否实现代理方法
    if ([self.delegate respondsToSelector:@selector(ImageeCellDeleteImageBtnClickedWithCell:)]) {
        //给代理发送消息
        [self.delegate ImageeCellDeleteImageBtnClickedWithCell:self];
    }
    
}

- (void)setItemImage:(UIImage *)itemImage{
    _itemImage = itemImage;
    //将传入的图片添加到按钮上(替换加号图片)
    [_imageBtn setBackgroundImage:!_itemImage ? [UIImage imageNamed:@"compose_pic_add"]:_itemImage forState:UIControlStateNormal];
    
    [_imageBtn setBackgroundImage:!_itemImage ? [UIImage imageNamed:@"compose_pic_add_highlighted"]:_itemImage forState:UIControlStateHighlighted];
    
    //处理删除按钮的显示状态
    _deleteImageBtn.hidden = !_itemImage;
    
    
    
}










@end
